<template>
  <body-page>
    <div class="body-page-add">
      <el-form class="search-form add-form" ref='form' :rules="rules" :model="submitData" label-width="100px">

        <el-form-item class="search-form-item" label="账号"  prop="account">
          <el-input v-model="submitData.account" placeholder="账号"></el-input>
        </el-form-item>

        <el-form-item class="search-form-item" label="角色" prop="roleCodeList">
          <role-select v-model="submitData.roleCodeList" :multiple="true"></role-select>
        </el-form-item>

        <el-form-item class="search-form-item" label="密码"  prop="password">
          <el-input type="password" v-model="submitData.password" placeholder="密码"></el-input>
        </el-form-item>

        <el-form-item class="search-form-item" label="确认密码"  prop="rePassword">
          <el-input type="password" v-model="submitData.rePassword" placeholder="确认密码"></el-input>
        </el-form-item>

        <el-form-item class="search-form-item" label="昵称"  prop="name">
          <el-input v-model="submitData.name" placeholder="昵称"></el-input>
        </el-form-item>

        <el-form-item class="search-form-item" label="手机号"  prop="phone">
          <el-input v-model="submitData.phone" placeholder="手机号"></el-input>
        </el-form-item>

        <el-form-item class="search-form-item" label="状态"  prop="status">
          <dict-select type-num="USER_STATUS" v-model="submitData.status"></dict-select>
        </el-form-item>

        <el-form-item class="search-form-item" label="审核状态"  prop="approveStatus">
          <dict-select type-num="USER_APPROVE_STATUS" v-model="submitData.approveStatus"></dict-select>
        </el-form-item>

        <el-form-item class="search-form-item" label="审核结果"  prop="approveResult">
          <dict-select type-num="USER_APPROVE_RESULT" v-model="submitData.approveResult"></dict-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submit('form')" >确定</el-button>
          <el-button @click="cancel">取消</el-button>
        </el-form-item>
      </el-form>
    </div>
  </body-page>
</template>

<script>
import BodyPage from '../../components/BodyPage'
import DictSelect from '../../components/DictSelect'
import RoleSelect from '../../components/RoleSelect'
export default {
  name: 'Add',
  components: {RoleSelect, DictSelect, BodyPage},
  data () {
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.submitData.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      submitData: {
        account: '',
        password: '',
        rePassword: '',
        name: '',
        phone: '',
        status: '',
        approveStatus: '',
        approveResult: '',
        roleCodeList: []
      },
      rules: {
        account: [
          { required: true, message: '请输入账号', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ],
        rePassword: [
          { validator: validatePass2, trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入昵称', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '请选择用户状态', trigger: 'blur' }
        ],
        approveStatus: [
          { required: true, message: '请选择审核状态', trigger: 'blur' }
        ],
        approveResult: [
          { required: true, message: '请选择审核结果', trigger: 'blur' }
        ],
        roleCodeList: [
          { required: true, message: '请选择用户角色', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    submit (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let vm = this
          this.$axios.post(vm.$API.user.add, vm.$qs.stringify(vm.submitData)).then(response => {
            if (response.data.code === 2000) {
              vm.$message.success(response.data.message)
              vm.cancel()
            } else {
              vm.$message.error(response.data.message)
            }
          }).catch(response => {
            console.log(response)
            vm.$message.error('系统异常')
          })
        }
      })
    },
    cancel () {
      this.$router.back()
    }
  }
}
</script>

<style scoped>

  .body-page-add {
    padding: 20px 10px;
    background-color: white;
  }

  .add-form {
    max-width: 500px;
  }

</style>
